<?php namespace Admin\Common;

/**
 * TODO: BizService 请求过来的数据处理
 * @author pom <pengsuangqi@aomai.com.au>
 */
class POMBizClient{

  // TODO:测试类函数用
  function testom()
  {
    echo "sb";exit;
  }

  // TODO: 这个动作是用于新增一个厂商预留给公司的商品
  public function NewExGoods($GoodsSn,$Price,$ExSupplierID)
  {
    $Price = round($Price,2);
    $params = array();
    $params["GoodsSn"] = $GoodsSn;
    $params["Price"] = $Price;
    $params["ExSupplierID"] = $ExSupplierID;

    // TODO: 判断这个SN是否已经有了
    $DAOHAND = M('ex_reserved_goods','jxc_','ExWarehouse');
    $where = array();
    $where["goods_sn"]    = $GoodsSn;
    // $where["Is_Deleted"]  = 0;
    $ret = $DAOHAND->where($where)->find();
    if(isset($ret) && !empty($ret)) return false;

    $DAOHAND = M('ex_reservation','jxc_','ExWarehouse');
    $where = array();
    $where["goods_sn"]    = $GoodsSn;
    // $where["is_deleted"]  = 0;
    $ret = $DAOHAND->where($where)->find();
    if(isset($ret) && !empty($ret)) return false;

    $url = POMNetTools::buildBizUrl("ExWarehouse","NewExGoods",$params);
    $res = POMNetTools::getData($url);
    if(!isset($res) || empty($res) || 0 != $res["RetCode"] ) return false;
    return true;
  }

  // TODO: 这个动作是用于移除一个厂商预留给公司的商品
  public function RemoveExGoods($GoodsSn)
  {
    $params = array();
    $params["GoodsSn"] = $GoodsSn;

    $url = POMNetTools::buildBizUrl("ExWarehouse","RemoveExGoods",$params);
    $res = POMNetTools::getData($url);

    if(!isset($res) || empty($res) || 0 != $res["RetCode"] ) return false;
    return true;
  }

  public function BatchFillPriceForIds($ids,$pom_fill_price)
  {
    //字符串转数组
    $tmp = explode(",", $ids);
    if( !is_array( $tmp ) ) return false;

    for( $i = 0 ; $i < count($tmp)-1 ; $i++ )
    {
      $this->AdjProcurePrice( $tmp[$i] , $pom_fill_price );
    }

    return true;
  }

  public function BatchFillQuantityForIds($ids,$pom_fill_num)
  {
    //字符串转数组
    $tmp = explode(",", $ids);
    if( !is_array( $tmp ) ) return false;

    for( $i = 0 ; $i < count($tmp)-1 ; $i++ )
    {
      $this->AdjReservation( $tmp[$i] , $pom_fill_num );
    }

    return true;
  }

  // TODO:批量填充价格
  public function BatchFillPrice($pre_goods_sn,$start_goods_sn,$end_goods_sn,$pom_fill_price )
  {
    //先获取所有商品SN ？
    $DAOHAND = M('ex_reserved_goods','jxc_','ExWarehouse');
    $where = array();
    $where["Is_Deleted"]  = 0;
    $ret = $DAOHAND->where($where)->field("goods_sn")->select();
    if(!isset($ret) || empty($ret)) return false;
    $snstr = "";
    foreach ($ret as $k => $v ) {
        $snstr .= $v["goods_sn"].',';
    }

    $i = intval($start_goods_sn);
    $wl = strlen(floor($end_goods_sn));
    for( ; $i <= intval($end_goods_sn) ; $i++ )
    {
      $temp = $pre_goods_sn.sprintf("%0".$wl."d", $i);

      if( false === strstr( $snstr , $temp) ){
        continue;
      }

      $this->AdjProcurePrice( $temp , $pom_fill_price );

    }

    return true;
  }

  public function BatchFillQuantity($pre_goods_sn,$start_goods_sn,$end_goods_sn, $pom_fill_num )
  {
    $DAOHAND = M('ex_reservation','jxc_','ExWarehouse');
    $where = array();
    $where["is_deleted"]  = 0;
    $ret = $DAOHAND->where($where)->field("goods_sn")->select();
    if(!isset($ret) || empty($ret)) return false;
    $snstr = "";
    foreach ($ret as $k => $v ) {
        $snstr .= $v["goods_sn"].',';
    }

    $i = intval($start_goods_sn);
    $wl = strlen(floor($end_goods_sn));
    for( ; $i <= intval($end_goods_sn) ; $i++ )
    {
      $temp = $pre_goods_sn.sprintf("%0".$wl."d", $i);

      if( false === strstr( $snstr , $temp) ){
        continue;
      }

      $this->AdjReservation( $temp , $pom_fill_num );

    }

    return true;


  }

  // TODO: 修改价格并返回 修改后的真实价格
  public function AdjProcurePriceAndRetPrice($GoodsSn,$NewPrice)
  {
    $this->AdjProcurePrice($GoodsSn,$NewPrice);
    $DAOHAND = M('ex_reserved_goods','jxc_','ExWarehouse');
    $where = array();
    $where["goods_sn"]    = $GoodsSn;
    $where["Is_Deleted"]  = 0;
    $ret = $DAOHAND->where($where)->find();
    if(isset($ret) && !empty($ret)) {
      return $ret["supply_price"];
    }
    return -1;
  }


  // TODO:这个动作是用于调整厂商预留给公司的商品价格的。
  public function AdjProcurePrice($GoodsSn,$NewPrice)
  {
    $NewPrice = round($NewPrice,2);
    $params = array();
    $params["GoodsSn"] = $GoodsSn;
    $params["NewPrice"] = $NewPrice;

    $url = POMNetTools::buildBizUrl("ExWarehouse","AdjProcurePrice",$params);
    $res = POMNetTools::getData($url);

    if(!isset($res) || empty($res) || 0 != $res["RetCode"] ) return false;
    return true;
  }


  // TODO: 修改数量并返回 修改后的真实数量
  public function AdjReservationAndRetReservation($GoodsSn,$NewReserveNumber)
  {
    $this->AdjReservation($GoodsSn,$NewReserveNumber);
    $DAOHAND = M('ex_reservation','jxc_','ExWarehouse');
    $where = array();
    $where["goods_sn"]    = $GoodsSn;
    $where["is_deleted"]  = 0;
    $ret = $DAOHAND->where($where)->find();
    if(isset($ret) && !empty($ret)) {
      return $ret["quantity"];
    }
    return -1;
  }

  // TODO: 这个动作是用于调整厂商预留给公司的商品数量的。
  public function AdjReservation($GoodsSn,$NewReserveNumber)
  {
    if( $NewReserveNumber < 0 ) return false;
    $params = array();
    $params["GoodsSn"] = $GoodsSn;
    $params["NewReserveNumber"] = intval( $NewReserveNumber );

    $url = POMNetTools::buildBizUrl("ExWarehouse","AdjReservation",$params);
    $res = POMNetTools::getData($url);

    if(!isset($res) || empty($res) || 0 != $res["RetCode"] ) return false;
    return true;
  }

  // TODO: 这个动作是用于复制一个订单到外部库存协同系统里。
  public function SyncOrder($SourceSysType,$OrderID)
  {

    $SourceSys = "aomai";
    if($SourceSysType == 1) $SourceSys = "m51";

    $params = array();
    $params["SourceSys"] = $SourceSys;
    $params["OrderID"] = intval( $OrderID );

    //判断这个订单ID是否已经同步
    $DAOHANDIF = M('ex_order_info','jxc_','ExWarehouse');
    $where = array();
    $where["Source_sys"]         = $SourceSysType + 1;
    $where["Source_order_id"]    = $params["OrderID"];
    $ret = $DAOHANDIF->where($where)->find();
    if(isset($ret) && !empty($ret)) return false;

    $url = POMNetTools::buildBizUrl("ExWarehouse","SyncOrder",$params);
    $res = POMNetTools::getData($url);

    if(!isset($res) || empty($res) || 0 != $res["RetCode"] ) return false;
    return true;
  }

  // TODO: 这个动作是用于生成一个厂商的最新的提货单
  public function GenCollection($ExSupplierID)
  {
    $params = array();
    $params["ExSupplierID"] = intval( $ExSupplierID );

    $url = POMNetTools::buildBizUrl("ExWarehouse","GenCollection",$params);
    $res = POMNetTools::getData($url);

    if(!isset($res) || empty($res) || 0 != $res["RetCode"] ) return false;
    return true;
  }

  // TODO: 合并提货单生成新的提货单
  public function CombinedBillLading( $ExSupplierID , $data )
  {
    if( empty($data) ) return false;
    // TODO: 调用BizService部分 预留 pom
    // 判断是否在同一个厂商
    $DAOHAND = M('ex_collection','jxc_','ExWarehouse');
    $where = array();
    $tmp = array();
    foreach ($data as $key => $value) {
      array_push( $tmp, $value["sn"]);
    }
    $where["collection_sn"] = array("in",$tmp);
    $ret = $DAOHAND->field("ex_supplier_id")->where($where)->select();

    $ret = POMNetTools::array_multunique( $ret , array('ex_supplier_id') );
    if( count( $ret ) > 1 ) return false;

    $params = array();
    $params["CollectionSnArrJson"] = urlencode( json_encode($data) ) ;
    $params["ExSupplierID"]        = $ExSupplierID;

    $url = POMNetTools::buildBizUrl("ExWarehouse","CombinedBillLading",$params);
    // POMNetTools::fileDebug( $url ,"");exit;
    $res = POMNetTools::getData($url);


    if(!isset($res) || empty($res) || 0 != $res["RetCode"] )  return false;
    return true;
  }

  // TODO: 保存提货单具体备货数量
  public function SaveCollectionGoodsNums( $CollectionId , $data )
  {
    // TODO: 调用BizService部分 预留 pom
    // 检测输入的实际备货数量是否大于 需要备货数量
    $DAOHAND = M('ex_collection_item','jxc_','ExWarehouse');
    $where = array();
    $where["ex_collection_id"] = intval( $CollectionId );
    $ret   = $DAOHAND->where($where)->select();
    foreach ($ret as $key => $value)
      if( ( $data[$key]["sn"] == $ret[$key]["goods_sn"] ) && ( $data[$key]["num"] > $ret[$key]["quantity"] ) )
        return false;

    $BRet   = true;
    $Totall = 0;
    for( $i = 0 ; $i < count( $data ) ; $i++ )
    {
      if( 0 == $data[$i]["checked"] ) continue;
      $params = array();
      $params["CollectionId"] = intval( $CollectionId );
      $params["GoodsSn"]      = $data[$i]["sn"];
      $params["GoodsId"]      = $data[$i]["gid"];
      $params["Number"]       = intval( $data[$i]["num"] );

      $url = POMNetTools::buildBizUrl("ExWarehouse","SaveCollectionGoodsNums",$params);
      $res = POMNetTools::getData($url);

      if(!isset($res) || empty($res) || 0 != $res["RetCode"] )
        $BRet = false;
      if( $BRet )
        $Totall += intval( $data[$i]["num"] );
    }

    // TODO: 设置这次总共备货数量
    if( $BRet ){
      $params = array();
      $params["CollectionId"] = intval( $CollectionId );
      $params["Number"]       = $Totall;
      $url = POMNetTools::buildBizUrl("ExWarehouse","ActualTotalStock",$params);
      $res = POMNetTools::getData($url);
    }

    return $BRet;
  }

  // TODO: 这个动作是用于供买方或卖方确认提货单提货完成。
  public function ConfirmCollection($PartyType,$CollectionId,$UserId)
  {
    $Party = "BUYER";
    if($PartyType == 1) $Party = "SELLER";
    $params = array();
    $params["Party"] = $Party;
    $params["CollectionId"] = intval( $CollectionId );
    $params["UserId"] = intval( $UserId );

    $url = POMNetTools::buildBizUrl("ExWarehouse","ConfirmCollection",$params);
    $res = POMNetTools::getData($url);

    if(!isset($res) || empty($res) || 0 != $res["RetCode"] ) return false;
    return true;
  }

  //TODO: 预留商品一个
  public function GetAsideProductDataOne($ex_goods_id)
  {
    $DAOHAND = M('ex_reserved_goods','jxc_','ExWarehouse');
    $where = array();
    if( isset($ExSupplierID) && 0 != $ExSupplierID )
      $where["cpinfo_id"] = $ExSupplierID;
    $where["ex_goods_id"] = $ex_goods_id;
    $where["Is_Deleted"] = 0;
    $res = $DAOHAND->where($where)->select();

    if(!isset($res) || empty($res) ) return array();

    foreach ($res as $key => $value) {
      $where = array();
      $where["goods_sn"] = $value['goods_sn'];
      $tmp = M("yw_goods")->where($where)->find();
      $res[$key]["goods_name"] = $tmp["goods_name"];
      $res[$key]["bar_code"] = $tmp["bar_code"];

    }
    return $res[0];
  }


  //TODO: 获取预留商品列表
  public function GetAsideProductDataList($ExSupplierID,$PreGoodsSn,$options)
  {
    $DAOHAND = M('ex_reserved_goods','jxc_','ExWarehouse');
    $where = array();
    if( 0 != $ExSupplierID )
      $where["cpinfo_id"] = $ExSupplierID;

    if( !empty( $PreGoodsSn ))
      $where["goods_sn"] = array("like","%".$PreGoodsSn."%");

    $where["Is_Deleted"] = 0;
    $res = $DAOHAND->where($where)->limit($options)->select();

    if(!isset($res) || empty($res) ) return array();

    foreach ($res as $key => $value) {
      $where = array();
      $where["goods_sn"] = $value['goods_sn'];
      $tmp = M("yw_goods")->where($where)->find();
      $res[$key]["goods_name"] = $tmp["goods_name"];
      $res[$key]["bar_code"] = $tmp["bar_code"];
    }
    return $res;
  }
  //TODO: 获取预留商品列表总共有多少数量
  public function GetAsideProductMaxNum($ExSupplierID,$PreGoodsSn)
  {
    $DAOHAND = M('ex_reserved_goods','jxc_','ExWarehouse');
    $where = array();
    if( 0 != $ExSupplierID )
      $where["cpinfo_id"] = $ExSupplierID;

    if( !empty( $PreGoodsSn ))
      $where["goods_sn"] = array("like","%".$PreGoodsSn."%");

    $where["Is_Deleted"] = 0;
    $res = $DAOHAND->where($where)->count();
    return intval($res);
  }

  //TODO: 获取预留商品列表总共有多少数量
  public function GetAsideProductReservationMaxNum($ExSupplierID,$PreGoodsSn)
  {
    $DAOHAND = M('ex_reservation','jxc_','ExWarehouse');
    $where = array();
    if( 0 != $ExSupplierID )
      $where["Supplier_id"] = $ExSupplierID;

    if( !empty( $PreGoodsSn ))
        $where["goods_sn"] = array("like","%".$PreGoodsSn."%");

    $where["is_deleted"] = 0;
    $res = $DAOHAND->where($where)->count();
    return intval($res);
  }

  // TODO: 获取预留商品 关于数量
  public function GetAsideProductReservation($ExSupplierID,$PreGoodsSn,$options)
  {
    $DAOHAND = M('ex_reservation','jxc_','ExWarehouse');
    $where = array();
    if( 0 != $ExSupplierID )
      $where["Supplier_id"] = $ExSupplierID;

    if( !empty( $PreGoodsSn ))
        $where["goods_sn"] = array("like","%".$PreGoodsSn."%");

    $where["is_deleted"] = 0;
    $res = $DAOHAND->where($where)->limit($options)->select();

    if(!isset($res) || empty($res) ) return array();

    foreach ($res as $key => $value) {
      $where = array();
      $where["goods_sn"] = $value['goods_sn'];
      $tmp = M("yw_goods")->where($where)->find();
      $res[$key]["goods_name"] = $tmp["goods_name"];
      $res[$key]["bar_code"] = $tmp["bar_code"];

    }
    return $res;

  }

  //TODO: 预留商品一个
  public function GetAsideProductDataReservationOne($ex_goods_id)
  {
    $DAOHAND = M('ex_reservation','jxc_','ExWarehouse');
    $where = array();
    if( isset($ExSupplierID) && 0 != $ExSupplierID )
      $where["Supplier_id"] = $ExSupplierID;
    $where["id"] = $ex_goods_id;
    $where["is_deleted"] = 0;
    $res = $DAOHAND->where($where)->select();
    if(!isset($res) || empty($res) ) return array();

    foreach ($res as $key => $value) {
      $where = array();
      $where["goods_sn"] = $value['goods_sn'];
      $tmp = M("yw_goods")->where($where)->find();
      $res[$key]["goods_name"] = $tmp["goods_name"];
      $res[$key]["bar_code"] = $tmp["bar_code"];

    }
    return $res[0];
  }

  // TODO: 提货单列表
  public function GetCollectionDataList($ExSupplierID,$options)
  {

    $DAOHAND = M('ex_collection','jxc_','ExWarehouse');
    $where = array();
    if( 0 != $ExSupplierID )
      $where["ex_supplier_id"] = $ExSupplierID;

    $where["item_number"] = array("GT",0);

    if( null == $options)
      $res = $DAOHAND->where($where)->order('create_time desc')->select();
    else
      $res = $DAOHAND->where($where)->limit($options)->order('create_time desc')->select();

    if(!isset($res) || empty($res) ) return array();

    return $res;
  }

  // TODO: 提货单总数
  public function GetCollectionDataNum($ExSupplierID)
  {

    $DAOHAND = M('ex_collection','jxc_','ExWarehouse');
    $where = array();
    if( 0 != $ExSupplierID )
      $where["ex_supplier_id"] = $ExSupplierID;

    $res = $DAOHAND->where($where)->count();
    return intval($res);
  }
  // TODO: 提货单最大ID
  public function GetCollectionMaxId($ExSupplierID)
  {
    $DAOHAND = M('ex_collection','jxc_','ExWarehouse');
    $where = array();
    if( 0 != $ExSupplierID )
      $where["ex_supplier_id"] = $ExSupplierID;

    $res = $DAOHAND->where($where)->max('ex_collection_id');
    return intval($res);
  }

  // TODO: 提货单到处excel
  public function GetCollectionDataExcel( $ExSupplierID )
  {
    $DAOHAND = M('ex_collection','jxc_','ExWarehouse');
    $where = array();
    if( 0 != $ExSupplierID )
      $where["ex_supplier_id"] = $ExSupplierID;

    $res = $DAOHAND->field("collection_sn,create_time,item_number,collection_amount,including_gst,is_collected,collection_number")->where($where)->order('create_time desc')->select();

    if(!isset($res) || empty($res) ) return array();

    foreach($res as $key=>$item){
        $res[$key]['create_time'] = $item['create_time'] ? date('Y-m-d H:i:s',$item['create_time']):'-';  //盘点时间
        $res[$key]['is_collected'] = $item['is_collected'] == 1  ? '已提货' :'未提货';  //盘点时间
        $res[$key]['is_collection_number'] = $item['item_number'] == $item['collection_number']  ? '是' :'否';
        if( $res[$key]['collection_number'] < 0 ) $res[$key]['collection_number'] = 0;
    }
    return $res;
  }

  // TODO: 提货单一个
  public function GetCollectionDataOne($ex_collection_id)
  {
    $DAOHAND = M('ex_collection','jxc_','ExWarehouse');
    $where = array();
    if( isset( $ExSupplierID ) && 0 != $ExSupplierID )
      $where["ex_supplier_id"] = $ExSupplierID;

    $where["ex_collection_id"] = $ex_collection_id;
    $res = $DAOHAND->where($where)->select();

    if(!isset($res) || empty($res) ) return array();

    return $res[0];
  }

  // TODO: 计算提货单合计
  public function GetCollectionAcount( $status , &$data = array() )
  {
      $ret = array();
      $txtTotalCollectionNum = 0;
      $txtTotalXiang         = 0;
      $txtTotalGst           = 0;
      $txtTotalCollect       = 0;
      for( $i = 0 ; $i < count($data) ; $i++)
      {
        $txtTotalCollectionNum += $data[$i]["collection_quantity"];
        $txtTotalXiang++;

        $data[$i]["txtTotalCollect"] = $data[$i]["collection_quantity"]*$data[$i]["unit_price"];
        $txtTotalCollect            += $data[$i]["txtTotalCollect"];
        $data[$i]["txtTotalGst"]     = number_format($data[$i]["collection_quantity"] * $data[$i]["taxrate"] / 1.1 ,2);
        $txtTotalGst                += $data[$i]["txtTotalGst"];
      }
      // TODO: 厂家还没有备货的 无备货合计
      if( $status == 0 )
      {
        $ret["txtTotalCollectionNum"] = 0;
        $ret["txtTotalXiang"]         = 0;
        $ret["txtTotalGst"]           = 0;
        $ret["txtTotalCollect"]       = 0;
      }else
      {
        $ret["txtTotalCollectionNum"] = $txtTotalCollectionNum;
        $ret["txtTotalXiang"]         = $txtTotalXiang;
        $ret["txtTotalGst"]           = $txtTotalGst;
        $ret["txtTotalCollect"]       = $txtTotalCollect;
      }
      return $ret;
  }

  // TODO: 提货单的商品
  public function GetCollectionGoodsDataList($ex_collection_id)
  {
    $DAOHAND = M('ex_collection_item','jxc_','ExWarehouse');
    $where = array();
    $where["ex_collection_id"] = $ex_collection_id;

    $res = $DAOHAND->where($where)->select();

    if(!isset($res) || empty($res) ) return array();

    return $res;
  }


  // TODO: 订单流水查询   获取总数 pom
  public function GetOrderFlowDataNum( $ExSupplierID ,$goods_sn, $order_sn , $source_order_sn , $startTime , $endTime )
  {
    $DAOHANDIT = M('ex_order_item','jxc_','ExWarehouse');

    $total = $DAOHANDIT->alias('t')->join('jxc_ex_order_info as f on f.ex_order_id=t.ex_order_id','left')
        ->where($this->_buildWhereForSelectOrderFlow($ExSupplierID , $goods_sn , $order_sn , $source_order_sn , $startTime , $endTime))->count();

    // echo  $total;exit;
    return $total;
  }
  // TODO: 订单流水查询   列表 pom
  public function GetOrderFlowDataList( $ExSupplierID , $options , $goods_sn, $order_sn , $source_order_sn , $startTime , $endTime )
  {
    $DAOHANDIT = M('ex_order_item','jxc_','ExWarehouse');

    $datalist = $DAOHANDIT->alias('t')->join('jxc_ex_order_info as f on f.ex_order_id=t.ex_order_id','left')
        ->where($this->_buildWhereForSelectOrderFlow($ExSupplierID , $goods_sn , $order_sn , $source_order_sn , $startTime , $endTime))
        ->order('f.create_time desc')
        ->limit($options)->select();

    return $datalist;
  }

  // TODO:
  private function _buildWhereForSelectOrderFlow($ExSupplierID , $goods_sn , $order_sn , $source_order_sn , $startTime , $endTime)
  {
    $where = array();

    if( isset( $ExSupplierID ) && 0 != $ExSupplierID )
      $where["t.ex_supplier_id"] = intval($ExSupplierID);

    if( isset( $goods_sn ) && "" != $goods_sn )
      $where["t.goods_sn"] = trim($goods_sn);

    if( isset( $order_sn ) && "" != $order_sn )
      $where["f.order_sn"] = trim($order_sn);
    if( isset( $source_order_sn ) && "" != $source_order_sn )
      $where["f.Source_order_sn"] = trim($source_order_sn);

    if( isset( $startTime ) && 0 != $startTime && false != $startTime){
      $tmpwg = array('EGT',trim($startTime));
      $where["f.create_time"] = array('EGT',trim($startTime));
    }
    if( isset( $endTime ) && 0 != $endTime && false != $endTime){
      $tmpwe = array('ELT',trim($endTime));
      $where["f.create_time"] = array('ELT',trim($endTime));
    }

    if( isset($tmpwg) && isset($tmpwe) )
      $where["f.create_time"] = array( $tmpwg, $tmpwe, 'and') ;

    return $where;
  }

  // TODO: 构建查询条件
  private function _buildWhereForSelectOrderFlowItem($ExSupplierID , $goods_sn)
  {
    $where = array();

    if( isset( $ExSupplierID ) && 0 != $ExSupplierID )
      $where["ex_supplier_id"] = intval($ExSupplierID);

    if( isset( $goods_sn ) && "" != $goods_sn )
      $where["goods_sn"] = trim($goods_sn);

    return $where;
  }
  private function _buildWhereForSelectOrderFlowInfo($ex_order_id, $order_sn , $source_order_sn , $startTime , $endTime )
  {
    $where = array();
    $where["ex_order_id"] = $ex_order_id;

    if( isset( $order_sn ) && "" != $order_sn )
      $where["order_sn"] = trim($order_sn);
    if( isset( $source_order_sn ) && "" != $source_order_sn )
      $where["Source_order_sn"] = trim($source_order_sn);

    if( isset( $startTime ) && 0 != $startTime && false != $startTime){
      $tmpwg = array('EGT',trim($startTime));
      $where["create_time"] = array('EGT',trim($startTime));
    }
    if( isset( $endTime ) && 0 != $endTime && false != $endTime){
      $tmpwe = array('ELT',trim($endTime));
      $where["create_time"] = array('ELT',trim($endTime));
    }

    if( isset($tmpwg) && isset($tmpwe) )
      $where["create_time"] = array( $tmpwg, $tmpwe, 'and') ;

    return $where;
  }


  // TODO: 导出Excel  考虑到全部导出  用程序来执行可能比数据来执行 效率要好
  public function GetOrderFlowDataExcel( $ExSupplierID ,$goods_sn, $order_sn , $source_order_sn , $startTime , $endTime)
  {
    $res = array(); $i = 0;
    $DAOHANDIF = M('ex_order_info','jxc_','ExWarehouse');
    $DAOHANDIT = M('ex_order_item','jxc_','ExWarehouse');

    $data = $DAOHANDIT->field("ex_order_id,goods_sn,goods_name,quantity,unit_price,line_amount,is_collected")
      ->where($this->_buildWhereForSelectOrderFlowItem($ExSupplierID , $goods_sn))->select();
    if(!isset($data) || empty($data)) return array();

    foreach ($data as $key => $value) {

      $temp  = $DAOHANDIF->field("order_sn,Source_sys,Source_order_sn")
        ->where($this->_buildWhereForSelectOrderFlowInfo($value["ex_order_id"],$order_sn , $source_order_sn , $startTime , $endTime))->find();

      if(isset($temp) && !empty($temp)){
        $data[$key] = array_merge($temp,$value);

        $res[$i]['order_sn'] = $data[$key]["order_sn"];
        $res[$i]['source_sys'] =  $data[$key]["source_sys"]== 1 ? "澳买" : "51m";
        $res[$i]['source_order_sn'] =  $data[$key]["source_order_sn"];
        $res[$i]['goods_sn'] = $data[$key]["goods_sn"];
        $res[$i]['goods_name'] = $data[$key]["goods_name"];
        $res[$i]['quantity'] = $data[$key]["quantity"];
        $res[$i]['unit_price'] = $data[$key]["unit_price"];
        $res[$i]['line_amount'] = $data[$key]["line_amount"];
        $res[$i++]['is_collected']= $data[$key]["is_collected"] == 0 ? "未提货" : "已提货";
      }
    }
    return $res;
  }


  private function _getSupplier()
  {
    $ret = array();
    $where = array();
    $where["group_key"] = "exwearehouse_users";
    $temp = M("auth_group")->where($where)->find();
    if(!isset($temp) || empty($temp) ) return $ret;
    $where = array();
    $where["group_id"] = $temp["id"];
    $tmp  = M("auth_group_access")->where($where)->select();
    if(!isset($tmp) || empty($tmp) ) return $ret;
    return $tmp;
  }

  // TODO: 获取厂商列表
  public function GetSupplierDataList()
  {
    $ret = array();
    $tmp = $this->_getSupplier();
    // TODO: 获取厂商账号数据
    foreach ($tmp as $key => $value) {
      $where = array();
      $where["id"] = $value["uid"];
      $data  = M("ucenter_member")->where($where)->find();
      array_push($ret,$data);
    }
    return $ret;
  }

  // TODO: 获取厂商列表
  public function GetSupplierNameList()
  {
    $ret = array();
    $tmp = $this->_getSupplier();
    // TODO: 获取厂商账号数据
    foreach ($tmp as $key => $value) {
      $where = array();
      $where["id"] = $value["uid"];
      $data  = M("ucenter_member")->where($where)->find();
      $name = "";
      if(empty($data["nickname"])) $name = $data["username"];
      else $name = $data["nickname"];

      $temp["id"]   = $value["uid"];
      $temp["name"] = $name;
      array_push($ret,$temp);
    }
    return $ret;
  }

}

?>
